// <%@ page pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>页面脚本 \\

// <%// 此行有秘密：%>权限树的数据<%="\n"%>var actionData = <%@ include file="edit_actionData.jsp" %>;

$(function() {

	var trees = [];
	var treeSetting = {
		check : {
			enable : true,
			chkboxType : {
				"Y" : "ps",
				"N" : "ps"
			}
		},
		data : {
			keep : {
				parent : true
			},
			simpleData : {
				enable : true
			}
		},
		callback : {
			// 单击勾选
			onClick : function(event, treeId, treeNode) {
				$.fn.zTree.getZTreeObj(treeId).checkNode(treeNode, !treeNode.checked, true, true);
			}
		}
	};

	// 登录成功，跳转
	function redirectAfterSave() {
		var url = "./list";
		window.open(url, "_self");
	}

	// 组装表单数据之前，设置 actionIds 的值
	function beforeSerialize() {
		var nodeIds = new Array();
		for (var i = 0; i < trees.length; i++) {
			var nodes = trees[i].getCheckedNodes(true);
			for (var j = 0; j < nodes.length; j++) {
				var n = nodes[j];
				if (!n.isParent) {
					nodeIds[nodeIds.length] = n.id;
				}
			}
		}
		$("#actionIds").val(nodeIds);
		$("#actionIds").attr("disabled", $("#actionIds").val() ? false : true);
	}

	// 提交前，禁用按钮，并显示 spin 动画
	function beforeSubmit(str, $form) {
		$("#btn-submit").attr("disabled", true);
		$("#ico-save").removeClass("fa-save");
		$("#ico-save").addClass("fa-spinner fa-spin");
	}

	// 处理登录结果
	function afterSubmit(responseText, statusText) {
		// 停500秒再显示结果，交互效果好一点
		window.setTimeout(function() {
			if (responseText.trim() == "") {
				$("#btn-submit-txt").text("已保存 Saved");
				window.setTimeout(redirectAfterSave, 500);
			} else {
				universal.modalWarning({
					title : "${dxfn:lang('保存失败', 'Faild to Save')}",
					msg : responseText
				});
				$("#btn-submit").attr("disabled", false);
				$("#ico-save").removeClass("fa-spinner fa-spin");
				$("#ico-save").addClass("fa-save");
			}
		}, 500);
	}

	function doInit() {

		$(".ztree").each(function(i) {
			trees[trees.length] = $.fn.zTree.init($(this), treeSetting, actionData[i]);
		});

		// 切换角色状态控件
		$("#state-btn-group").radioBtnGroup("${ role.isDisabled ? '1' : '0' }");

		// 模块下拉列表项，把模块名字设置到按钮上
		$('#switch-tree-dropdown a[data-toggle="tab"]').on('shown.bs.tab', function(e) {
			$("#btn-switch-tree-dropdown").html($(this).html());
		});

		// 显示隐藏所有模块
		$("#btn-switch-all-tree").click(function() {
			$("#tree-tab-content .tab-pane").removeClass("active").removeClass("in");
			var btn = $(this);
			if (btn.is(".active")) {
				btn.removeClass("active");
				$("#btn-switch-tree-dropdown").html("${ dxfn:lang('请选择模块', 'Please Select a Module') }");
			} else {
				btn.addClass("active");
				$("#tree-tab-content .tab-pane").addClass("active").addClass("in");
				$("#btn-switch-tree-dropdown").html("${ dxfn:lang('所有模块', 'All Modules') }");
			}
		});

		// 验证表单，成功后通过 ajax 提交
		$('#role-form').validator({
			timely : true,
			focusCleanup : true,
			theme : 'yellow_bottom',
			valid : function(form) {
				$(form).ajaxSubmit({
					beforeSerialize : beforeSerialize,
					beforeSubmit : beforeSubmit,
					success : afterSubmit
				});
			}
		});

	}

	// 页面加载，初始化
	$(document).ready(doInit);

});